package com.haiyuancloud.base.ctr;

import com.haiyuancloud.base.deal.ctr.BaseCtr;
import com.haiyuancloud.common.redis.service.CaffeineCacheService;
import com.haiyuancloud.rest.model.ClearCacheResultModel;
import com.haiyuancloud.util.ObjectUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Log4j
@RestController
@RequestMapping("/clear")
public class ClearCacheCtr extends BaseCtr {

    @Resource
    private CaffeineCacheService caffeineCacheService;

    @PostMapping("/clearCache")
    public ClearCacheResultModel clearCache(@RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        Map<String, Object> submitData = this.getDefaultResultData();
        String clearCacheKey = ObjectUtil.toString(submitMap.get("clearCacheKey")).replace("\"","");
        List<String> clearCacheList = ObjectUtil.toList(clearCacheKey,",");
        caffeineCacheService.remove(clearCacheList);
        ClearCacheResultModel clearCacheResultModel  = new ClearCacheResultModel();
        clearCacheResultModel.setResult("清除成功");
        return clearCacheResultModel;
    }
}
